﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ROrganizer.Client.Files;

namespace ROrganizer.Client
{
    public class ApplicationSettings
    {
        internal static readonly string[] SupportedExtensions = new[] { ".avi", ".mkv" };

        private readonly List<string> folders;

        internal ApplicationSettings(string[] folders)
        {
            this.folders = new List<string>(folders);
        }

        public CopySettings DefaultCopySettings { get; set; }
        public RemoveSettings DefaultRemoveSettings { get; set; }

        public string[] GetFolders()
        {
            return this.folders.ToArray();
        }

        public void AddFolder(string folder)
        {
            this.folders.Add(folder);
        }

        public void RemoveFolder(string folder)
        {
            this.folders.Remove(folder);
        }

        public static ApplicationSettings GetInstance()
        {
            using (ApplicationData.OpenSession(ApplicationData.Settings))
            {
                var document = ApplicationData.GetDocument(ApplicationData.Settings);
                var copySettings = new CopySettings { RenameUsingImdbTitles = true };
                var removeSettings = new RemoveSettings { RemoveAssociatedFiles = true };

                var folders = from folder in document.Root.Elements("Folder")
                              select folder.Value;

                if (document.Root.Element("CopySettings") != null)
                {
                    copySettings.DestinationFolder = document.Root.Element("CopySettings").Element("DestinationFolder").Value;
                    copySettings.RenameUsingImdbTitles = Boolean.Parse(document.Root.Element("CopySettings").Element("RenameUsingImdbTitle").Value);
                }

                if (document.Root.Element("RemoveSettings") != null)
                {
                    removeSettings.RemoveAssociatedFiles = Boolean.Parse(document.Root.Element("RemoveSettings").Element("RemoveAssociatedFiles").Value);
                }

                return new ApplicationSettings(folders.ToArray())
                {
                    DefaultCopySettings = copySettings,
                    DefaultRemoveSettings = removeSettings,
                };
            }
        }

        public static void Persist(ApplicationSettings settings)
        {
            using (ApplicationData.OpenSession(ApplicationData.Settings))
            {
                var document = new XDocument(
                                new XElement("ROrganizer",
                                    new XElement("CopySettings",
                                        new XElement("DestinationFolder", new XCData(settings.DefaultCopySettings.DestinationFolder)),
                                        new XElement("RenameUsingImdbTitle", settings.DefaultCopySettings.RenameUsingImdbTitles)),
                                    new XElement("RemoveSettings",
                                        new XElement("RemoveAssociatedFiles", settings.DefaultRemoveSettings.RemoveAssociatedFiles)),
                                    from folder in settings.GetFolders()
                                    select new XElement("Folder", new XCData(folder))));

                ApplicationData.UpdateDocument(ApplicationData.Settings, document);
            }
        }
    }
}
